package com.ntanh.mealplanner.main.objects;

import android.content.ContentValues;
import android.util.Log;

import com.ntanh.mealplanner.base.model.BaseEntity;
import com.ntanh.mealplanner.main.dao.DataBaseHelper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Anh Nguyen on 2/24/2015.
 */
public class Meal extends BaseEntity implements Comparable<Meal>{

    public static final String MEAL_TYPE = "mealType";

    public static final String RECIPE_LIST = "recipeList";

    private MealType type;

    private List<Recipe> recipeList;

    public Meal(String author, MealType type, List<Recipe> recipeList) {
        super(author);
        this.type = type;
        this.recipeList = recipeList;
    }

    public Meal(String author) {
        super(author);
        this.recipeList = new ArrayList<Recipe>();
    }

    public Meal(String author, MealType type) {
        super(author);
        this.type = type;
        this.recipeList = new ArrayList<Recipe>();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Meal)) return false;
        if (!super.equals(o)) return false;

        Meal meal = (Meal) o;

        if (type != meal.type) return false;

        if (recipeList != null && meal.recipeList != null && recipeList.size() == meal.recipeList.size()) {
            for (int i = 0; i < recipeList.size(); i++) {
                if (!recipeList.get(i).equals(meal.recipeList.get(i))) {
                    return false;
                }
            }
        } else {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (type != null ? type.hashCode() : 0);
        if (recipeList != null) {
            int hash = 0;
            for (Recipe recipe : recipeList) {
                hash += recipe.hashCode();
            }
            result = 31 * result + hash;
        }
        return result;
    }

    public MealType getType() {
        return type;
    }

    public void setType(MealType type) {
        this.type = type;
    }

    public List<Recipe> getRecipeList() {
        return recipeList;
    }

    public void setRecipeList(List<Recipe> recipeList) {
        this.recipeList = recipeList;
    }

    public JSONObject toJSONObject() {

        JSONObject json = super.toJSONObject();
        try {
            if (json != null) {
                json = new JSONObject();
            }
            json.put(Meal.MEAL_TYPE, type.getName());
            if (recipeList != null) {
                JSONArray array = new JSONArray();

                for (Recipe recipe : recipeList) {
                    array.put(recipe.toJSONObject());
                }

                json.put(Meal.RECIPE_LIST, array);
            }

        } catch (Exception e) {
            Log.d("Convert error", "Cannot convert Meal to JSON", e);
        }

        return json;
    }

    public void toContentValue(ContentValues values, boolean forInserting) {
        super.toContentValue(values, forInserting);
        values.put(MEAL_TYPE, getType().getName());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < recipeList.size() - 1; i++) {
            sb.append(recipeList.get(i).getId());
            sb.append(BaseEntity.entitySplitSign);
        }
        if (recipeList.size() > 0) {
            sb.append(recipeList.get(recipeList.size() - 1).getId());
        }
        values.put(RECIPE_LIST, sb.toString());
    }

    @Override
    public int compareTo(Meal another) {
        return this.type.compareTo(another.getType());
    }

    @Override
    public String getTableName() {
        return DataBaseHelper.MEAL_TABLE;
    }
}
